package com.future.mayi.qsale.utils;

import android.content.Context;

import com.future.baselib.utils.ApiCst;
import com.future.baselib.utils.HttpRequest;
import com.future.mayi.qsale.model.UploadMutilFileResponse;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/8/20.
 */

public class UploadMutilImgUtils {

    private Context mContext;
    private UploadListener mUploadListener;
    private List<List<String>> uploadImgList;
    private List<String> resultImgList;
    private int index;

    public UploadMutilImgUtils(Context context, UploadListener uploadListener) {
        this.mContext = context;
        this.mUploadListener = uploadListener;
        resultImgList = new ArrayList<>();
    }

    public void startUpload(List<List<String>> imgLists) {
        this.uploadImgList = imgLists;
        index = 0;
        upload();
    }

    private void upload() {
        new HttpRequest<UploadMutilFileResponse>()
                .with(mContext)
                .setApiCode(ApiCst.mutiUploadPic)
                .setMutiFilePath(uploadImgList.get(index))
                .setListener(new HttpRequest.OnNetworkListener<UploadMutilFileResponse>() {
                    @Override
                    public void onSuccess(UploadMutilFileResponse response) {
                        resultImgList.add(response.getResponseString());
                        if (resultImgList.size() >= uploadImgList.size()) {
                            if (mUploadListener != null) {
                                mUploadListener.uploadSucceed(resultImgList);
                            }
                        } else {
                            index++;
                            upload();
                        }
                    }

                    @Override
                    public void onFail(String message, UploadMutilFileResponse response) {
                        if (mUploadListener != null) {
                            mUploadListener.uploadFailed(message);
                        }
                    }
                })
                .uploadMutiImage(new UploadMutilFileResponse());
    }

    public interface UploadListener {

        void uploadSucceed(List<String> imgString);

        void uploadFailed(String message);
    }

}
